package com.example.ywj.toutiao.utils;

import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.ywj.toutiao.R;

import java.util.List;

public class IssueAdapter extends BaseAdapter {
    private static final String TAG = "IssueAdapter";
    public List<Uri> list;
    public IssueAdapter(List<Uri> list){
        this.list = list;
    }
    @Override
    public int getCount() {
        int count = list == null?1:list.size()+1;
        Log.i(TAG, "getCount: "+list);
        if(count>9){
            return list.size();
        }else{
            return count;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.issue_item, null);
            viewHolder = new ViewHolder();
            viewHolder.iv = convertView.findViewById(R.id.issue_iv);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if(position<list.size()){
            viewHolder.iv.setImageURI(list.get(position));
        }else{
            viewHolder.iv.setImageResource(R.drawable.add_images);

        }


        return convertView;
    }
    class ViewHolder{
        ImageView iv;
    }
}
